package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.BaseReportLossDetailMapper;
import com.kaili.basic.mapper.BaseReportLossMapper;
import com.kaili.basic.pojo.dto.BaseReportLossAddDetailDTO;
import com.kaili.basic.pojo.entity.BaseReportLossDetailEntity;
import com.kaili.basic.pojo.entity.BaseReportLossEntity;
import com.kaili.basic.pojo.entity.BaseStoreLocationEntity;
import com.kaili.basic.service.BaseReportLossDetailService;
import com.kaili.basic.service.BaseReportLossService;
import com.kaili.basic.service.BaseStoreLocationService;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class BaseReportLossDetailServiceImpl extends ServiceImpl<BaseReportLossDetailMapper, BaseReportLossDetailEntity> implements BaseReportLossDetailService {

    @Autowired
    private BaseStoreLocationService baseStoreLocationService;

    @Override
    public void insertBatch(List<BaseReportLossAddDetailDTO> detailList, String orderNo) {
        List<BaseReportLossDetailEntity> list=new ArrayList<>();
        //查询本库位的默认报损库位
        List<BaseStoreLocationEntity> locations = baseStoreLocationService.selectByMap(new HashMap<String, Object>() {{
            put("store_id", detailList.get(0).getStoreId());
            put("store_location_type", 215);
            put("default_location", 0);
        }});
        for (BaseReportLossAddDetailDTO detail:detailList) {
            BaseReportLossDetailEntity detailEntity=new BaseReportLossDetailEntity();
            BeanUtils.copyProperties(detail,detailEntity);
            detailEntity.setOrderNo(orderNo);
            detailEntity.setLossLocationId(locations.get(0).getId().longValue());
            detailEntity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
            detailEntity.setCreateTime(new Date());
            list.add(detailEntity);
        }
        this.insertBatch(list);
    }
}
